package com.acme.bankapp.domain.bank;

import com.acme.exceptions.NotEnoughFundsException;
import com.acme.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount implements Account {

	private double overdraft;

	public CheckingAccount(final double amount, final double overdraft) {
		super(amount);
		this.overdraft = overdraft;
	}

	@Override
	public void deposit(final double amount) {
		setBalance(getBalance() + amount);
	}

	@Override
	public void withdraw(final double amount)
			throws OverDraftLimitExceededException, NotEnoughFundsException {
		if (amount < 0) {
			throw new IllegalArgumentException("Amount can not be negative");
		}
		
		if (this.balance > amount){
			this.balance -= amount;
			return;
		} 
		
		double overdraftNeeded = amount - this.balance;
		
		if (overdraft < overdraftNeeded) {
			throw new OverDraftLimitExceededException(amount, this);
		} else {
			this.balance = 0.0;
			overdraft -= overdraftNeeded;
			throw new NotEnoughFundsException();
		}
		
	}
		
		
		
	public double getOverDraft(){
		return this.overdraft;
	}
		

		

	@Override
	public double maximumAmountToWithdraw() {
		return getBalance() + this.overdraft;
	}

}
